--
-- @Author:      name
-- @DateTime:    2018-03-30 23:05:48
-- @Description: 机器人逻辑

local skynet = require "skynet"
local log = require "Log"
local Network = require "Network"
local roomConfig = require "lua_ZjhTableList"
local NodeMessage = require "NodeMessage"
local Rule = require "Rule"
local configProb = require "ConfigRobot"

require "setting"
local Logic = class("Logic")

---------------------------------------------------------
-- Private
---------------------------------------------------------
function Logic:ctor(data)
	--随机种子
    local time = tostring(os.time())
    math.randomseed(time:reverse():sub(1, 7))
	local svr_config = robot_setting['zhajinhua']
	self.playerid = data.mid
	self.player = nil
	-- self.user = user
	self.ip = svr_config.ip
	self.port = svr_config.port		
	self.tbConf = {} --场次配置
	self.robotType = data.type
	for _,v in pairs(roomConfig) do 
		table.insert(self.tbConf,v)
	end
	table.sort(self.tbConf,function (a,b)
		if a.min_coin < b.min_coin then 
			return true
		end
	end)
	self.roomConf = self.tbConf[self.robotType] --要入的场
	self.onlineTime = 0
	self.ExitTime = math.random(10,30) * 60 --退出游戏的时间
	self.playingTime = 0
	self.leaveTableTime = math.random(3,9) * 60 --离开桌子的时间
	self.operNum = 0
	self.tbCard = {} --所有玩家的牌
	self.card = {} --自己的牌
	self.tbRes = {} --牌结果
	self.followProbability = 100 --跟注的概率
	self.lookProbability = 0 --看牌概率
	self.bStart = false --游戏是否开始
	self.looked = false --是否已看牌
	self.bottom_num = 0.01
	self.addChipList = {'0.02','0.05','0.1'}

	self.nodeMessage = NodeMessage.new()
	self.rule = Rule.new()
	--开启网络模块
	self.network = Network.new(self.ip,self.port)
	--注册消息回调
	self:register()

	self:login(self.playerid)
	self:closeByTime()
end


function Logic:register()
	self.network:register('php_res',handler(self,self.php_res))
	self.network:register('logout_res',handler(self,self.logout_res))
	self.network:register('room_state_res',handler(self,self.room_state_res))
	self.network:register('enter_table_res',handler(self,self.enter_table_res))
	self.network:register('leave_table_res',handler(self,self.leave_table_res))	
	self.network:register('changetable_res',handler(self,self.changetable_res))		
	self.network:register('reconnect_res',handler(self,self.reconnect_res))
	self.network:register('nt_goods_change',handler(self,self.nt_goods_change))
	self.network:register('nt_enter_gate',handler(self,self.nt_enter_gate))
	self.network:register('exit',handler(self,self.exit))
	self.network:register('nt_kick_table',handler(self,self.nt_kick_table))
	self.network:register('niuniu_nt_seat_change',handler(self,self.niuniu_nt_seat_change))
	self.network:register('nt_broadcast',handler(self,self.nt_broadcast))	

	self.network:register('zjh_nt_rest',handler(self,self.zjh_nt_rest))
	self.network:register('zjh_nt_start',handler(self,self.zjh_nt_start))
	self.network:register('zjh_nt_oper',handler(self,self.zjh_nt_oper))
	self.network:register('zjh_nt_end',handler(self,self.zjh_nt_end))
	self.network:register('zjh_nt_compare',handler(self,self.zjh_nt_compare))	
	self.network:register('zjh_nt_compare',handler(self,self.zjh_nt_seat_change))	

	self.network:register('zjh_status_res',handler(self,self.zjh_status_res))
	self.network:register('zjh_oper_res',handler(self,self.zjh_oper_res))
	self.network:register('zjh_look_res',handler(self,self.zjh_look_res))
	self.network:register('zjh_compare_res',handler(self,self.zjh_compare_res))
	self.network:register('zjh_com_seat_res',handler(self,self.zjh_com_seat_res))	
	self.network:register('zjh_giveup_res',handler(self,self.zjh_giveup_res))	

end

function Logic:exit()
	self.network:close()
	skynet.exit()
end

function Logic:showError(data)
	if data.err_no and data.err_no > 0 then 
		print("_____error__",data)
		self:exit()
		return false
	end
	return true
end


--账号
function Logic:login(mid)
	local str = '{"ver":"1.0.0","appid":10100100,"action":"user.guestLogin","platform":1,"cmd":10000}'
	if mid then 
		str = '{"ver":"1.0.0","appid":10100100,"mid":'..mid..',"action":"user.guestLogin","platform":1,"cmd":10001}'
	end

	local data = {php_req={
 		id = 10000,
 		content = str,
	}}
	-- print(data)
	self.network:send(data)
end

function Logic:php_res(data)
	if not self:showError(data) then 
		return 
	end

end

function Logic:nt_enter_gate(data)
	--进入大厅成功
	local delay = math.random(100,300)
	skynet.sleep(delay)
	--进入场次
	self:enterTable(self.roomConf.id)
end

function Logic:logout_req()
	local data = {logout_req={playerid=1}}
	self.network:send(data)
end
function Logic:logout_res(data)
	if not self:showError(data) then 
		return 
	end
	self:login(self.playerid)
end

function Logic:room_state_req(gameid )
	local data = {room_state_req={
 		gameid=gameid, 		
	}}
	print(data)
	self.network:send(data)
end
function Logic:room_state_res(data)


end

function Logic:enterTable(room_type)
	local data = {enter_table_req={
 		room_type=room_type, 		
	}}
	self.network:send(data)		
end
function Logic:enter_table_res(data)
	if data.err_no and data.err_no > 0 then 
		print("____________金币不足___",self.playerid,self.robotType)
		data = {playerid=self.playerid,robotType=self.robotType}
		self.nodeMessage:sendService('manager','change_money',data)
		self:exit()
		return 
	end
	if data.tableid > 0 then 
		self.tableid = data.tableid
		self:zjhStatusReq()
	end
end

function Logic:reconnect_req()
	local data = {reconnect_req={
 		tableid=self.tableid, 		
	}}
	self.network:send(data)
end
function Logic:reconnect_res(data)
	print("#####reconnect_res#####",data)
end

function Logic:zjhStatusReq()
	local data = {zjh_status_req={
		tableid = self.tableid,
	}}
	self.network:send(data)
end
function Logic:zjh_status_res(data)
	-- print("__________ zjh_status_res",data)
	if not self:showError(data) then 
		return 
	end	

	local players = data.seat_player or {}
	for k,v in pairs(players) do
		if v.playerid == self.playerid then 
			self.player = v
			self.card = v.card_data		
		end		
		if v.card_data then 
			self.tbCard[v.playerid] = v.card_data
		end
	end
	if self.bStart then 
		--取到所有人的牌
		local weight
		local cardTypeName
		for k,v in pairs(self.tbCard) do 
			self.tbRes[k] = self.rule:getCardWeight(v)
		end
		weight = self.tbRes[self.playerid].weight
		cardTypeName = self.rule:getCardTypeText(weight)
		print("___cardTypeName___",cardTypeName)
		self.followProbability = configProb[cardTypeName].follow_prob 
		self.lookProbability = configProb[cardTypeName].look_prob 
		self.compareProbability = configProb[cardTypeName].compare_prob 
	else
		if next(data.config) and data.config.bottom_num then 
			self.bottom_num = data.config.bottom_num
			print("__________ zjh_status_res",data)
		end		
	end
	
end

function Logic:zjh_nt_rest(data)
	self.bStart = false
	if self.onlineTime > self.ExitTime then 
		self:exit()
		return
	end			
	if self.onlineTime > self.leaveTableTime then 
		-- print("___onlineTime________________",self.onlineTime , self.leaveTableTime)
		self.leaveTableTime = self.leaveTableTime + self.onlineTime
		skynet.sleep(math.random(100,200))
		local randNum = math.random(1,3)		
		if randNum == 2 then 
			self:leaveTableReq()
			return 
		else		
		end				
	end
end

function Logic:zjh_nt_compare(data)

end

--离开桌子
function Logic:leaveTableReq()
	local data = {
		leave_table_req = {
			tableid = self.tableid,
		}
	}
	self.network:send(data)
end
function Logic:leave_table_res(data)
	if not self:showError(data) then 
		return 
	end
	skynet.sleep(math.random(1000,2000))
	--进入场次
	self:enterTable(self.roomConf.id)	
end

function Logic:zjh_nt_start(data)
	self.bStart = true
	self.looked = false
end

function Logic:zjh_nt_oper(data)
	-- print("__________ zjh_nt_oper",data)

	if data.cur_oper.seatid ~= self.player.seatid then
		return 
	end
	local button_list = data.cur_oper.button_list 
	local addChipList = data.cur_oper.button_bet
	local randNum = math.random(1,100)
	local operList = {}
	local canLook = false
	local canCompare = false
	local followChip = addChipList[1] --当前跟注值
	local operRound = data.oper_round
	for k,v in pairs(button_list) do
		if v == 1 or v == 2 then 
			table.insert(operList,v)
		end
		if v == 3 then 
			canLook = true
		end
		if v == 4 then 
			canCompare = true
		end
	end
	skynet.sleep(math.random(80,600))	
	if operRound > 5 then 
		--第5轮后
		self.followProbability = self.followProbability + 5
	end
	-- print("____randNum___",randNum,self.followProbability,self.looked)
	if canCompare and randNum < self.compareProbability then 
		local data = {zjh_compare_req={
			tableid = self.tableid,
		}}
		self.network:send(data)
		return 
	elseif randNum > self.followProbability and self.looked then 
		--弃牌
		local data = {zjh_giveup_req={
			tableid = self.tableid,
		}}
		self.network:send(data)
		return 
	else
		local operType = math.random(1,#operList)
		local addChip = 0
		if operType == 2 then --加注
			local tb = {}
			for k,v in pairs(addChipList) do 
				if tonumber(v) > tonumber(followChip) then 
					table.insert(tb,v)
				end
			end
			if next(tb) then 
				addChip = tb[math.random(1,#tb)]
			else
				operType = 1				
			end
		end
		local data = {zjh_oper_req={
			tableid = self.tableid,
			oper_type = operType,
			add_chip = addChip,
		}}
		-- print(data,self.addChipList)
		self.network:send(data)		
	end
	if canLook and not self.looked then 
		--看牌
		randNum = math.random(1,100)
		print("_22222222___randNum___",randNum,self.lookProbability)
		if randNum < self.lookProbability then 
			local data = {zjh_look_req={
				tableid = self.tableid,
			}}
			self.network:send(data)
		end
	end

end
function Logic:zjh_oper_res(data)

end

function Logic:zjh_look_res(data)
	
	if data.err_no == 0 then 
		self.looked = true
	end
	-- print("___看牌了____",data.err_no,self.looked)
end
function Logic:zjh_compare_res(data)
	self:zjh_com_seat_req(data.seat_list[1])
end


function Logic:zjh_nt_end(data)

end

function Logic:zjh_giveup_res(data)
	
end

function Logic:zjh_com_seat_req(seatid)
	local data = {
		zjh_com_seat_req = {
		tableid = self.tableid,
		seatid = seatid,
		}
	}
	print("__111111111___zjh_com_seat_req________")
	self.network:send(data)
end
function Logic:zjh_com_seat_res(data)

end

--换桌
function Logic:changetable_req()
	local data = {
		changetable_req={tableid = self.tableid}
	}
	self.network:send(data)
end
function Logic:changetable_res(data)

end

--座位发生改变
function Logic:zjh_nt_seat_change()
	
end

function Logic:nt_goods_change()
	
end

function Logic:nt_kick_table(data)
	--被踢出房间
	skynet.exit()
end

--座位发生改变
function Logic:niuniu_nt_seat_change()
	
end

function Logic:nt_broadcast(data)
end



function Logic:closeByTime()
	--3 - 10分钟后退出游戏
	skynet.fork(function ()
		while (true) do 
			skynet.sleep(100)
			self.onlineTime = self.onlineTime + 1
			
		end
	end)
end



return Logic